package cn.qyd.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 邱运铎
 * @date 2024-03-12 1:36
 */
public class SocketServerDemo {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(8888);) {
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();

            Scanner scanner = new Scanner(is);
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(os,"gbk"),true);
            printWriter.println("你好啊！，欢迎关注【邱运铎】，回复1024领取Java进阶资源包");

            boolean done = true;

            while (done && scanner.hasNext()) {
                String line = scanner.nextLine();
                System.out.println(line);
                if("1024".equals(line)) {
                    done = false;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
